//
//  CollectionPage.m
//  TodayComment
//
//  Created by 张吉阳 on 15/10/8.
//  Copyright (c) 2015年 xianrenmoshi. All rights reserved.
//

#import "CollectionPage.h"
#import "FxDBManager.h"
#import "ConllectCell.h"
#import "CommentInfo.h"
#import "PCStackMenu.h"
#import "EGORefreshTableHeaderView.h"
#import "DetailPage.h"
@interface CollectionPage ()

@end

@implementation CollectionPage

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _collectionTableView.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    self.arrayInfo = (NSMutableArray *)[FxDBManager fetchNews];
    _collectionTableView.nightBackgroundColor = UIColorFromRGB(0x343434);
    _collectionTableView.nightSeparatorColor = UIColorFromRGB(0x313131);
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.view addSubview:_tabBar];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [_tabBar removeFromSuperview];
}


- (void)tabBar:(FxTabBar *)tabBar didSelectIndex:(NSInteger)index
{
    
    if (index == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }


}

- (void)nightFalls {
    [DKNightVersionManager nightFalling];
}

- (void)dawnComes {
    [DKNightVersionManager dawnComing];
}


#pragma mark - UITableViewDelegate & UITableViewDataSource methods

-(void)tableView:(UITableView *)tableView  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayInfo.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CommentInfo *info = nil;
   ConllectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ConllectCell"];
    info = [_arrayInfo objectAtIndex:indexPath.row];
    if (cell == nil) {
        NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:@"ConllectCell" owner:tableView options:nil];
        cell = [Objects objectAtIndex:0];
        [cell initCell];
    }
    [cell setCellData:info];
    return cell;

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentInfo *info = [_arrayInfo objectAtIndex:indexPath.row];
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        [FxDBManager delectNews:info.ID];
        [self.arrayInfo removeObjectAtIndex:indexPath.row];
        [_collectionTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailPage *page = [[DetailPage alloc] init];
    CommentInfo *info = [_arrayInfo objectAtIndex:indexPath.row];
    page.conllectJson = [FxDBManager fetchNews:info.ID];
    [self.navigationController pushViewController:page animated:YES];
}


@end
